package com.example.materialdesigndemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.core.view.GravityCompat
import com.example.materialdesigndemo.adapter.NewsAdapter
import com.example.materialdesigndemo.entity.News
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity() {

    private var adapter: NewsAdapter? = null
    private var newsList: List<News>? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //使用Toolbar(通过嵌套AppbarLayout可以实现更多功能)
        setSupportActionBar(toolbar)
        //设置侧边的按钮
        supportActionBar?.apply {
            setDisplayHomeAsUpEnabled(true)
            setHomeAsUpIndicator(R.drawable.ic_menu_24dp)
        }
        //侧边栏设置选中的项目
        navigation.setCheckedItem(R.id.nav_backup)
        navigation.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.nav_backup -> toast("点击了侧边栏备份")
                R.id.nav_delete -> toast("点击了侧边栏删除")
                R.id.nav_settings -> toast("点击了侧边栏设置")
            }
            drawerLayout.closeDrawers()
            true
        }
        //通过inflateHeaderView可以加入头部布局也可以通过XML属性
        navigation.inflateHeaderView(R.layout.nav_header).setOnClickListener {
            toast("点击了侧边栏头部")
            drawerLayout.closeDrawers()
        }
        //悬浮按钮点击事件
        fab.setOnClickListener {
            //Snackbar使用，这边在布局中可以使用CoordinatorLayout（与FrameLayout类似）在MaterialDesign中来监听变化给出合适的布局变化
            //使得弹出Snackbar不会遮挡FloatActionButton
            Snackbar.make(it, "点击了悬浮按钮", Snackbar.LENGTH_SHORT)
                .setAction("撤销") {
                    toast("点击了撤销")
                }
                .show()
        }

        newsList = getNewsList()
        adapter = NewsAdapter(newsList!!)
        newsListRecyclerView.adapter = adapter
        adapter?.setOnNewsItemClickListener(object : NewsAdapter.NewsItemClickListener {
            override fun onClick(position: Int) {
                val news = newsList!!.get(position)
                startActivity<NewsActivity>(NewsActivity.NEWS_TITLE to news.title,NewsActivity.NEWS_CONTENT to news.content)
            }
        })

        //下拉刷新
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary)
        swipeRefresh.setOnRefreshListener {
            refreshNewsList()
        }
    }

    private fun refreshNewsList() {
        Thread {
            Thread.sleep(2000)
            newsList = getNewsList()
            runOnUiThread {
                toast("刷新数据完成")
                adapter?.notifyDataSetChanged()
                //停止刷新
                swipeRefresh.isRefreshing = false
            }
        }.start()
    }

    private fun getNewsList(): List<News> {
        val newsList = ArrayList<News>()
        for (i in 1..50) {
            val news = News(
                title = "This is news title $i",
                content = "This is news content $i"
            )
            newsList.add(news)
        }
        return newsList
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                drawerLayout.openDrawer(GravityCompat.START)
            }
            R.id.backup -> toast("点击了备份")
            R.id.delete -> toast("点击了删除")
            R.id.settings -> toast("点击了设置")
        }
        return true
    }
}
